/**
 * @author emylyano3
 *
 */
package com.gui.windows;

import java.awt.Dimension;

import com.gui.windows.components.*;
import com.design.*;
import com.gui.*;
import com.resources.Resources;

import javax.swing.*;

/**
 * Singleton class that manage the application main window.
 * */
public class MainWindow extends FrameWindow implements Sizes, GUIComponent, UserActions {

	//**************************************************************************************************
	//	Constructor.
	//**************************************************************************************************

	private static MainWindow mainWindow = null;

	private MainWindow(String name) {
		super(name);
	}

	public static MainWindow getMainWindow (String name) {
		if (mainWindow == null) {
			mainWindow = new MainWindow(name);
		}
		return mainWindow;
	}

	public static MainWindow getMainWindow () {
		if (mainWindow == null) {
			mainWindow = new MainWindow("");
		}
		return mainWindow;
	}

	//**************************************************************************************************
	//	Attributes.
	//**************************************************************************************************

	/** Components of the main window. */
	private ToolPanel toolPanel;
	private SelectionPanel selectionPanel;
	private StatePanel statePanel;

	/** The design manager will take care of all the action that are been made into the template */
	private DesignManager designManager;

	/** Main Bar of the main window. */
	private JMenuBar menuBar;
	
	private static final long serialVersionUID = 1L;
	
	public static int lastUserCommand = -1;

	// Panel dimensions.
	public static final int GAPS = 60;
	public static final int MINIMUM_PANEL_WIDTH = StatePanel.MINIMUM_PANEL_WIDTH + SelectionPanel.MINIMUM_PANEL_WIDTH + GAPS;
	public static final int MINIMUM_PANEL_HEIGHT = StatePanel.MINIMUM_PANEL_HEIGHT + SelectionPanel.MINIMUM_PANEL_HEIGHT + GAPS;
	public static final int MAXIMUM_PANEL_WIDTH = StatePanel.MAXIMUM_PANEL_WIDTH + SelectionPanel.MAXIMUM_PANEL_WIDTH;
	public static final int MAXIMUM_PANEL_HEIGHT = StatePanel.MAXIMUM_PANEL_HEIGHT + SelectionPanel.MAXIMUM_PANEL_HEIGHT;
	
	//**************************************************************************************************
	//	Methods that exports this Class (Public Methods).
	//**************************************************************************************************

	/**
	 * Creates the application main window, that will contains the tools panels, the canvas and
	 * the menu bar.
	 * */
	public void createAndShowGUI(){
		initComponents();
		setLayout();
		addComponents();
		// main window settings.
		this.setJMenuBar(menuBar);
		this.setIconImage(UtilsGUI.loadImage(Resources.ICON_RESOURCES + Resources.MAIN_WINDOW_ICON_NAME + Resources.ICON_EXTENSION));
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
		this.setResizable(!false);
		this.pack();
	}
	
	/**
	 * Updates the states of the panels that are contained by the main window.</br>
	 * The action to be taken depends on the command passed as parameter.
	 * @param command The command to decide which update action is going to be taken.
	 * */
	public void update(int command) {
		lastUserCommand = command;
		switch (command) {
		case CREATE_TEMPLATE:
			statePanel.update(command);
			toolPanel.update(command);
			selectionPanel.update(command);	// to update the selection combo box in this panel.
			break;
		case TYPE_SELECTION_CHANGED:
		case FILTERING_CHANGED:
			statePanel.update(command);
			selectionPanel.update(command);
			break;
		case ITEM_SELECTION_CHANGED:
			selectionPanel.update(command);
			statePanel.update(command);
			break;
		case ITEM_CREATED:
			selectionPanel.update(command);
			statePanel.update(command);
			break;
		default:
			lastUserCommand = -1;	// command was not recognized.
			break;
		}
	}
	
	//**************************************************************************************************
	//* Private Methods. 
	//**************************************************************************************************
	
	/**
	 * Initialize all the components that are going to be contained by the main window.
	 * */
	protected void initComponents () {
		designManager = DesignManager.getDesignManager();
		toolPanel = new ToolPanel();
		selectionPanel = new SelectionPanel();
		statePanel = new StatePanel("State Panel");
		menuBar = MenuBar.getMenuBar(this);
	}
	
	/**
	 * Adds the components to the main window.
	 * */
	protected void addComponents() {
		layout.setHorizontalGroup(layout.createSequentialGroup()
				.add(layout.createParallelGroup()
						.add(toolPanel)
						.add(selectionPanel))				
				.add(statePanel)
		);
		layout.setVerticalGroup(layout.createParallelGroup()
				.add(layout.createSequentialGroup()
						.add(toolPanel)
						.add(selectionPanel))	
				.add(statePanel)
		);
		setDimensions();
	}
	
	private void setDimensions() {
		selectionPanel.setMinimumSize(new Dimension (SelectionPanel.MINIMUM_PANEL_WIDTH	,SelectionPanel.MINIMUM_PANEL_HEIGHT));
		selectionPanel.setMaximumSize(new Dimension (SelectionPanel.MAXIMUM_PANEL_WIDTH	,SelectionPanel.MAXIMUM_PANEL_HEIGHT));
		statePanel.setMinimumSize(new Dimension (StatePanel.MINIMUM_PANEL_WIDTH	,StatePanel.MINIMUM_PANEL_HEIGHT));
		statePanel.setMaximumSize(new Dimension (StatePanel.MAXIMUM_PANEL_WIDTH	,StatePanel.MAXIMUM_PANEL_HEIGHT));
		this.setMinimumSize(new Dimension (MINIMUM_PANEL_WIDTH ,MINIMUM_PANEL_HEIGHT));
		this.setMaximumSize(new Dimension (MAXIMUM_PANEL_WIDTH ,MAXIMUM_PANEL_HEIGHT));
	}
	
	//**************************************************************************************************
	//* Getters and setters. 
	//**************************************************************************************************

	public ToolPanel getToolPanel() {
		return this.toolPanel;
	}

	public SelectionPanel getSelectionPanel() {
		return this.selectionPanel;
	}

	public StatePanel getStatePanel() {
		return this.statePanel;
	}

	public DesignManager getDesignManager() {
		return this.designManager;
	}

	public void update() {}
	
	public void create(){}
	
	public void initFields() {}
	
	//**************************************************************************************************
	//* Components handler classes. 
	//**************************************************************************************************
	/**

	 * Handles the events of the set image button contained in the tool panel.  

	 * */
}
